# # from rest_framework.generics import ListAPIView
# # from rest_framework.views import APIView
# # from rest_framework.response import  Response
# #
# #
# # from rest_framework.viewsets import ModelViewSet
# #
# # from users.models import  User
# # from goods.models import GoodsVisitCount,SKU,SKUSpecification,SPU,SPUSpecification
# # from meiduo_admin.serializers.sku_serializers import *
# # from meiduo_admin.pages import MyPage
# #
# # class SKUGoodView(ModelViewSet):
# #     queryset = SKU.objects.all()
# #     serializer_class = SKUGoogsSerializers
# #     pagination_class = MyPage
# #
# #
# #     def get_queryset(self):
# #         if self.action == 'categories':
# #             return GoodsCategory.objects.filter(parent_id__gt=37)
# #         # elif self.action == 'simple':
# #         #     return SPU.objects.all()
# #         # elif self.action == 'specs':
# #         #     return SPUSpecification.objects.filter(spu_id=self.kwargs['pk'])
# #         else:
# #             keyword = self.request.query_params.get('keyword')
# #             if keyword == '' or keyword is None:
# #                 return SKU.objects.all()
# #             else:
# #                 return SKU.objects.filter(name=keyword)
# #
# #
# #     def get_serializer_class(self):
# #         if self.action == 'categories':
# #             return GoodCategorySerializer
# #         # elif self.action == 'simple':
# #         #     return SPUSimpleSerializer
# #         # elif self.action == 'specs':
# #         #     return SPUSpecSerializer
# #         return self.serializer_class
# #
# #     def categories(self,request):
# #         category_3_queryset = self.get_queryset()
# #         s = self.get_serializer(category_3_queryset,many=True)
# #         return Response(s.data)
# #
# #
# #     # def simple(self,request):
# #     #     spu_queryset =  self.get_queryset()
# #     #     s = self.get_serializer(spu_queryset,many=True)
# #     #     return Response(s.data)
# #
# # class SPUSimpleView(ListAPIView):
# #     queryset = SPU.objects.all()
# #     serializer_class = SPUSimpleSerializer
# #
# #
# #
# #
# # class SPUSpecSimpleView(ListAPIView):
# #     queryset = SPUSpecification.objects.all()
# #     serializer_class = SPUSpecSerializer
# #
# #
# #     def get_queryset(self):
# #         # 如何在非视图函数中，提取分组参数： 1、命名分组；2、非命名分组
# #         # /goods/(?P<name>\w+)/specs/  -->  self.kwargs.get("name")
# #         # /goods/(\w+)/(\d+)/specs/  -->  self.args[0],self.args[1]...
# #
# #         spu_id = self.kwargs.get('pk')
# #         return self.queryset.filter(spu_id=spu_id)
#
#
# from rest_framework.viewsets import ModelViewSet
# from meiduo_admin.serializers.sku_serializers import *
# from goods.models import SKU,GoodsCategory
# from meiduo_admin.pages import MyPage
# from rest_framework.response import Response
# from rest_framework.generics import ListAPIView
#
#
# class SKUViewSet(ModelViewSet):
#     queryset = SKU.objects.all()
#     serializer_class = SKUModelSerializer
#     pagination_class = MyPage
#
#
#     def get_queryset(self):
#         # 判断如果处理请求的视图函数是categories，那么应该返回三级分类查询集
#         # 视图对象.action属性：处理请求的视图函数的名称
#         if self.action == "categories":
#             return GoodsCategory.objects.filter(parent_id__gt=37)
#
#         # 原来的逻辑
#         keyword = self.request.query_params.get("keyword")
#         if keyword:
#             return self.queryset.filter(name__contains=keyword)
#         return self.queryset.all()
#
#
#     def get_serializer_class(self):
#         # 如果处理请求的视图是categories，返回GoodsCategoryDetailSerializer
#         if self.action == "categories":
#             return GoodsCategoryDetailSerializer
#
#         return self.serializer_class
#
#
#     def categories(self, request):
#         # 序列化返回三级分类信息
#         # 1、获得三级分类对象查询集
#         category_3_queryset = self.get_queryset()
#         # 2、构建序列化器对象
#         s = self.get_serializer(category_3_queryset, many=True)
#         # 3、序列化返回
#         return Response(s.data)
#
#
#
#
# class SPUSimpleView(ListAPIView):
#     queryset = SPU.objects.all()
#     serializer_class = SPUSimpleSerializer
#
#
#
#
# class SPUSpecSimpleView(ListAPIView):
#     queryset = SPUSpecification.objects.all()
#     serializer_class = SPUSpecSerializer
#
#
#     def get_queryset(self):
#         # 如何在非视图函数中，提取分组参数： 1、命名分组；2、非命名分组
#         # /goods/(?P<name>\w+)/specs/  -->  self.kwargs.get("name")
#         # /goods/(\w+)/(\d+)/specs/  -->  self.args[0],self.args[1]...
#
#         spu_id = self.kwargs.get('pk')
#         return self.queryset.filter(spu_id=spu_id)



from rest_framework.viewsets import ModelViewSet
from meiduo_admin.serializers.sku_serializers import *
from goods.models import SKU,GoodsCategory
from meiduo_admin.pages import MyPage
from rest_framework.response import Response
from rest_framework.generics import ListAPIView


# class SKUViewSet(ModelViewSet):
#     queryset = SKU.objects.all()
#     serializer_class = SKUModelSerializer
#     pagination_class = MyPage
#
#
#     def get_queryset(self):
#         # 判断如果处理请求的视图函数是categories，那么应该返回三级分类查询集
#         # 视图对象.action属性：处理请求的视图函数的名称
#         if self.action == "categories":
#             return GoodsCategory.objects.filter(parent_id__gt=37)
#
#         # 原来的逻辑
#         keyword = self.request.query_params.get("keyword")
#         if keyword:
#             return self.queryset.filter(name__contains=keyword)
#         return self.queryset.all()
#
#
#     def get_serializer_class(self):
#         # 如果处理请求的视图是categories，返回GoodsCategoryDetailSerializer
#         if self.action == "categories":
#             return GoodsCategoryDetailSerializer
#
#         return self.serializer_class
#
#
#     def categories(self, request):
#         # 序列化返回三级分类信息
#         # 1、获得三级分类对象查询集
#         category_3_queryset = self.get_queryset()
#         # 2、构建序列化器对象
#         s = self.get_serializer(category_3_queryset, many=True)
#         # 3、序列化返回
#         return Response(s.data)
#
#
#
#
# class SPUSimpleView(ListAPIView):
#     queryset = SPU.objects.all()
#     serializer_class = SPUSimpleSerializer




# class SPUSpecSimpleView(ListAPIView):
#     queryset = SPUSpecification.objects.all()
#     serializer_class = SPUSpecSerializer
#
#
#     def get_queryset(self):
#         # 如何在非视图函数中，提取分组参数： 1、命名分组；2、非命名分组
#         # /goods/(?P<name>\w+)/specs/  -->  self.kwargs.get("name")
#         # /goods/(\w+)/(\d+)/specs/  -->  self.args[0],self.args[1]...
#
#         spu_id = self.kwargs.get('pk')
#         return self.queryset.filter(spu_id=spu_id)








# sku模型类视图集
class SKU___SkuModelViewSet(ModelViewSet):
    queryset = SKU.objects.all()
    serializer_class = SKU___SkuModelSerializer
    pagination_class = MyPage

    #重写，若有携带查询参数keyword，搜索功能。
    def get_queryset(self):
        keyword = self.request.query_params.get('keyword')
        if keyword == '' or keyword is None:
            return SKU.objects.all()
        else:
            return SKU.objects.filter(name__contains=keyword)

class SKU___SkuCategoryView(ListAPIView):
    queryset = GoodsCategory.objects.filter(parent_id__gt=37)
    serializer_class = SKU___CategorySimpleSerializer

class SKU___SkuSpuSimpleView(ListAPIView):
    queryset = SPU.objects.all()
    serializer_class = SKU___SkuSpuSimpleSerializer
#
class SKU___SkuSpuSpecView(ListAPIView):
    queryset = SPUSpecification.objects.all()
    serializer_class = SKU___SkuSpuSpecSimpleSerializer

    def get_queryset(self):
        pk = self.kwargs.get('pk')
        return SPUSpecification.objects.filter(spu_id=pk)


